package com.heima.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constant.GlobalConstant;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dots.UserRelationDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dots.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserFanService;
import com.heima.user.service.ApUserFollowService;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.MD5Utils;
import com.heima.utils.threadLocal.AppThreadLocalUtil;
import lombok.extern.flogger.Flogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser>implements ApUserService {
    @Autowired
    ApUserFanService apUserFanService;
    @Autowired
    ApUserFollowService apUserFollowService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public ResponseResult login(LoginDto loginDto) {
        Map<String,Object> map=new HashMap<>();
        if(StringUtils.isNotBlank(loginDto.getPassword())&&StringUtils.isNotBlank(loginDto.getPhone()))
        {
            ApUser user = this.getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone,loginDto.getPhone()));
            if(user==null)
            {
                throw new CustomException(AppHttpCodeEnum.USER_NOT_EXIST);
            }
            String password= DigestUtils.md5DigestAsHex((loginDto.getPassword()+user.getSalt()).getBytes());
            if(!user.getPassword().equals(password))
            {
                throw new CustomException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            String token= AppJwtUtil.getToken(user.getId().longValue());
            user.setSalt("");
            user.setPassword("");
            map.put(GlobalConstant.USER_TAG,user);
            map.put(GlobalConstant.TOKEN_TAG,token);
        }
        else{
            map.put(GlobalConstant.TOKEN_TAG,AppJwtUtil.getToken(0L));
        }
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult userFollow(UserRelationDto dto) {
        if (dto==null||dto.getArticleId()==null||dto.getAuthorId()==null||dto.getOperation()==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        if (dto.getOperation().equals(UserRelationDto.Operation.FOLLOW.getValue()))
        {
            if(!apUserFanService.addFan(AppThreadLocalUtil.getUserId(), dto.getAuthorId()))
                return ResponseResult.errorResult();
        }
        else{
            if (!apUserFanService.removeFan(AppThreadLocalUtil.getUserId(),dto.getAuthorId()))
                return ResponseResult.errorResult();
        }
        return ResponseResult.okResult();
    }
}
